import * as express from 'express'
import logger from '../logs'
import collectionRouter from './collection'
import rootRouter from './root'

const errorRequestHandler = (err, _, res, __) => {
  logger.error(err.stack)

  res.json({
    error: err.message || err.toString(),
  })
}

const api = (app: express.Express) => {
  app.use('/', rootRouter, errorRequestHandler)
  app.use('/api/collection', collectionRouter, errorRequestHandler)
}

export default api
